import {
    API_getIdentityList,//获取身份列表（用于获取管理员类型）
    API_getUserIdentity,//获取指定身份用户列表
    API_getCollegeList,//获取学院列表（大学）
    API_getMajorList,//获取专业列表（大学）
    API_getGroupList,//获取教学组列表（区分大中小学）
    API_getUserTeachers,//获取教师列表
    API_getGradeList,//获取年级列表（区分大中小学）
    API_getClassList,//获取班级列表（区分大中小学）
    API_getUserStudents,//获取学生用户
    API_getUserParents,//获取家长用户
    API_getDepartmentList,//获取部门及列表
    API_getDepartmentStaffs,//获取部门员工列表
    API_getClassMine,//获取我的班级列表
    API_getGroupMine,//获取我的教师组列表
    API_getUserSearch,//搜索用户
    API_receiverCache,//刷新选择对象
} from '../../../../network/servers'
import getInfo from '../../utils/getInfo'

function api_getIdentityList(data, receivers){
    return new Promise<any>((resolve, reject) => {
        let params = {
            schoolId:getInfo.getSchoolId(),
            userType:0
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getIdentityList(params, header).then(res=>{
            console.log("API-获取管理员身份列表:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.identityName,
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })
    })
}
/**
 * 获取学院列表
 * @param data 对象: type
 * @param identityType 身份类型：1/教师；2/学生和家长
 */
function api_getCollegeList(data, identityType){//获取学院列表
    return new Promise<any>((resolve, reject) => {
        let params = {
            identityType:identityType,
            schoolId:getInfo.getSchoolId()
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getCollegeList(params,header).then(res=>{
            console.log("API-获取学院列表:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.collegeName,
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })
    })
}

function api_getGroupList(data){//获取教学组列表
    return new Promise<any>((resolve, reject) => {
        let params = {
            schoolType:getInfo.getSchoolType(),
            schoolId:getInfo.getSchoolId()
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getGroupList(params,header).then(res=>{
            console.log("API-获取教学组列表:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.subjectName + "教师组",
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })
    })
}

function api_getGradeList(data){
    return new Promise<any>((resolve, reject) => {
        let params = {
            schoolType:getInfo.getSchoolType(),
            schoolId:getInfo.getSchoolId()
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getGradeList(params,header).then(res=>{
            console.log("API-获取年级列表:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.gradeName,
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        }) 
    })
}

function api_getDepartmentList(data){
    return new Promise<any>((resolve, reject) => {
        let params = {
            sysId:getInfo.getSysId(),
            schoolId:getInfo.getSchoolId()
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getDepartmentList(params,header).then(res=>{
            console.log("API-获取学校组织结构:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.departmentName,
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })
    })
}

function api_getClassMine(data){
    return new Promise<any>((resolve, reject) => {
        let params = {
            schoolType:getInfo.getSchoolType(),
            userId:getInfo.getUserId(),
            schoolId:getInfo.getSchoolId(),
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getClassMine(params,header).then(res=>{
            console.log("API-获取我的班级列表:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.className,
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })  
    })
}

function api_getGroupMine(data){
    return new Promise<any>((resolve, reject) => {
        let params = {
            schoolType:getInfo.getSchoolType(),
            userId:getInfo.getUserId(),
            schoolId:getInfo.getSchoolId()
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getGroupMine(params,header).then(res=>{
            console.log("API-获取我的教研组:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.groupName || item.subjectName,
                        subData:undefined,
                        hasCho:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })
    })
}

function api_getClassList(data){
    return new Promise<any>((resolve, reject) => {
        let params:any = {
            schoolType:getInfo.getSchoolType(),
            sysId:getInfo.getSysId(),
            schoolId:getInfo.getSchoolId(),
            // gradeId:data.Info.gradeId,
        }
        if(getInfo.getSchoolType() == 1){//大学(传专业ID)
            params.majorId = data.Info.majorId
        }else{//中小学（传年级ID）
            params.gradeId = data.Info.gradeId
        }
        let header = {
            "Authorization":getInfo.getToken()
        }
        API_getClassList(params, header).then(res=>{
            console.log("API-获取班级列表:",res,"\nparams:",params)
            if(res.code == 200){
                let handleRes = res.data.map((item,index)=>{
                    return {
                        name:item.className,
                        subData:undefined,
                        hasCho:data.hasCho == 1?1:0,
                        index,
                        type:[...(data.type||[]), index],
                        Info:item
                    }
                })
                resolve(handleRes)
            }
        })
    })
}

export default {
    api_getIdentityList,//获取管理员身份列表
    api_getCollegeList,//获取学院列表
    api_getGroupList,//获取教学组列表
    api_getGradeList,//获取年级列表
    api_getDepartmentList,//获取学校组织结构
    api_getClassMine,//获取我的班级列表
    api_getGroupMine,//获取我的教研组列表
    api_getClassList,//根据年级id或者专业id获取班级列表
}